﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Configuration;

using NAudio;
using NAudio.Wave;

namespace HPKaraoke
{
    public class SongGUI
    {

        public ucLyricDisplayControl Lyric1 { get; set; }
        public ucLyricDisplayControl Lyric2 { get; set; }

        public SongController song { get; set; }


        public bool IsPlaying { get; set; }
        public bool  IsPausing { get; set; }

        private bool _wasSetControl = false;

        System.Windows.Threading.DispatcherTimer dispatcherTimer;
        TimeSpan start;
        TimeSpan now;
        TimeSpan startDelay;
        TimeSpan totalDelay = new TimeSpan(0,0,0,0,100);
        private bool disposed = false;

        MediaEndedFunction _mediaEndedFunc;

        protected SongGUI() 
        {
            SetTimer();
            IsPlaying = false;
            IsPausing = false;
        }

        public SongGUI(string parentPath, string configFileName):this()
        {
            song = new XmlSongController(parentPath, configFileName);
            
        }

        public SongGUI(SongController s,ucLyricDisplayControl lyric1, ucLyricDisplayControl lyric2)
            :this()
        {

            SetControl(lyric1, lyric2);
            song = s; 
        }

        public void SetControl(ucLyricDisplayControl lyric1, ucLyricDisplayControl lyric2)
        {
            this.Lyric1 = lyric1;
            this.Lyric2 = lyric2;
            _wasSetControl = ((lyric1 != null) && (lyric2 != null));
        }

        public SongGUI(string configFileName, ucLyricDisplayControl lyric1, ucLyricDisplayControl lyric2)
            :this()
        {

            song = new XmlSongController(ConfigurationSettings.AppSettings["ZingDataPath"] ,configFileName);
            SetControl(lyric1, lyric2); 
        }

        private void SetTimer()
        {
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(Update);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 0);
        }

        virtual public void Play()
        {
            dispatcherTimer.IsEnabled = true;
            dispatcherTimer.Start();
            start = new TimeSpan(DateTime.Now.Ticks);
            IsPlaying = true;
        }


        public virtual void PauseOrResume()
        {
            if (dispatcherTimer.IsEnabled)
                startDelay = new TimeSpan(DateTime.Now.Ticks);
            else
                totalDelay = (new TimeSpan(DateTime.Now.Ticks)).Subtract(startDelay);
            dispatcherTimer.IsEnabled = !dispatcherTimer.IsEnabled;
            IsPausing = !IsPausing;
        }

        public virtual void Stop()
        {
            dispatcherTimer.Stop();
            IsPlaying = false;
        }


        int currentLyricID = 0;

        public virtual void Update(object sender, EventArgs e)
        {
            now = new TimeSpan(DateTime.Now.Ticks);
            song.Update(now.Subtract(start).Subtract(totalDelay));

            if (!song.Finish)
            {
                Sentence s = song.GetSentenceByTime(now.Subtract(start).Subtract(totalDelay));
                if (s != null)
                {
                    s.Update(now.Subtract(start).Subtract(totalDelay));


                    currentLyricID = song.SentenceIndicator % 2;

                    Sentence nextSentence = song.GetNextSentenceByTime(now.Subtract(start).Subtract(totalDelay));
                    if (nextSentence != null)
                        SetSentenceSinging(s.RawData, s.WordWasSinging, nextSentence.RawData);
                    else
                        SetSentenceSinging(s.RawData, s.WordWasSinging, "");

                }
                else
                { 
                    
                }
            }
            else
            {
                _mediaEndedFunc();
            }
        }

        private void SetSentenceSinging(string currentSentence, string currentWord, string nextSentence)
        {
            if (!_wasSetControl)
            {
                throw new Exception("Hasn't set control for display lyrics");
            }

            if (currentLyricID == 0)
            {
                Lyric1.BackgroundText.Text = currentSentence;
                Lyric1.AnimationText.Text = currentWord;
                Lyric2.BackgroundText.Text = nextSentence;
                Lyric2.AnimationText.Text = "";
            }
            else
            {
                Lyric2.BackgroundText.Text = currentSentence;
                Lyric2.AnimationText.Text = currentWord;
                Lyric1.BackgroundText.Text = nextSentence;
                Lyric1.AnimationText.Text = "";
            }
        }

        virtual public void SetMediaEndedFunction(MediaEndedFunction f)
        {
            _mediaEndedFunc = f;
        }

        public void Dispose()
        {
            Stop();
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Manual release of managed resources.
                }
                // Release unmanaged resources.
                disposed = true;
            }
        }

        /// <summary>
        /// Trả về 0 nếu đang stop, 1 nếu đang play, 2 nếu đang pause
        /// </summary>
        /// <returns></returns>
        public virtual int GetPlaybackState()
        {
            return 0;  
        }
        ~SongGUI() { Dispose(true); }
    }
}
